package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/getCount")
public class UserFindAllGetCountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String sex = req.getParameter("sex");
        String startBirthday = req.getParameter("startBirthday");
        String endBirthday = req.getParameter("endBirthday");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String enable = req.getParameter("enable");
        String hobby = req.getParameter("hobby");


        if (!"".equals(hobby) && ",".equals(Character.toString(hobby.charAt(hobby.length() - 1)))) {
            hobby = hobby.substring(0, hobby.length() - 1);
        }
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setSex(sex);
        userModel.setStartBirthday(startBirthday);
        userModel.setEmail(email);
        userModel.setEndBirthday(endBirthday);
        userModel.setHobby(hobby);
        userModel.setEnable(enable == null || enable.equals("") ? -1 : Integer.parseInt(enable));
        userModel.setPhone(phone);

        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");
//        PageModel pageModel = new PageModel();
//        pageModel.setCurPage(Integer.parseInt(curPage));
//        pageModel.setPageSize(Integer.parseInt(pageSize));
//        Integer page = curPage == null || "".equals(curPage) ? 1 : Integer.parseInt(curPage);
//        Integer size = pageSize == null || "".equals(pageSize) ? 1 : Integer.parseInt(pageSize);
        userModel.setPageModel(new PageModel(Tool.stringToInteger(curPage), Tool.stringToInteger(pageSize)));


        IUserService iUserService = new UserServiceImpl();

        int totalCount=iUserService.getCount(userModel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("totalCount",totalCount);
        resp.getWriter().println(jsonObject);
    }
}
